package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.junit.Test;
import www.lagou.entity.Account;
import www.lagou.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class AccountDao {

    // 通过 卡号 查询 account 信息
    @Test
    public Account findAccountByCard(Connection conn,String card) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from account where card = ?";

        Account account = qr.query(conn,sql,new BeanHandler<>(Account.class),card);

        return account;
    }

    // 通过 卡号 查询 account 信息
    @Test
    public Account findAccountByCard(String card) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "select * from account where card = ?";

        Account account = qr.query(sql,new BeanHandler<>(Account.class),card);

        return account;
    }

    // 更新 account 的 当前余额
    @Test
    public boolean updateAccountBalance(Account account) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "update account set balance = ? where card = ?";

        Object[] objects = {account.getBalance(),account.getCard()};

        int i  = qr.update(sql,objects);

        return i > 0;
    }

    // 更新 account 的 当前余额
    @Test
    public boolean updateAccountBalance(Connection conn,Account account) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "update account set balance = ? where card = ?";

        Object[] objects = {account.getBalance(),account.getCard()};

        int i  = qr.update(conn,sql,objects);

        return i > 0;
    }
}
